package com.routes;

import com.controllers.ClientController;
import com.controllers.JobController;
import com.controllers.StorageController;
import com.exceptions.JobNotFoundException;
import com.models.Client;
import com.models.Job;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Rest {

  @Autowired
  private JobController jobController;

  @Autowired
  private StorageController storageController;

  @Autowired
  private ClientController clientController;

  @RequestMapping(value = "/jobs/{id}", method = RequestMethod.GET)
  public Job getJobDetails(@PathVariable("id") String id) throws JobNotFoundException {
    return jobController.getJob(id);
  }

  @RequestMapping(value = "/process_job", method = RequestMethod.POST)
  public Job postSync(@RequestBody String request) throws ParseException {
    return jobController.processJob(request);
  }

  @Secured("ROLE_ADMIN")
  @RequestMapping(value = "/new_client", method = RequestMethod.POST)
  public Client postNewClient(@RequestBody String request) throws IOException {
    return clientController.saveClient(request);
  }

  @RequestMapping(value = "/get_file/{filename}", method = RequestMethod.GET, produces = "application/pdf")
  public FileSystemResource getFile(@PathVariable("filename") String filename, HttpServletResponse response) throws FileNotFoundException {
    return storageController.getFile(filename, response);
  }

}
